//引包
const express = require('express');
const expressStatic = require('express-static');
const dbConnect = require('./utils/db/mongdbConnect');
const slideRouter=require('./routers/slide');
const userRouter = require('./routers/user');
const bodyParser = require('body-parser');
const cookiesParser = require('cookie-parser');
const cookieSession = require('cookie-session');
const viewRouter = require('./routers/views');
const childRouter = require('./routers/child');
const menRouter = require('./routers/mens');
const womenRouter = require('./routers/women');
const mobileService = require('./routers/mobile');


//创建服务
var app = express();

//创建监听
app.listen(8080,function (err) {
    if(!err){
        console.log('server is running...');
    }
});
//开启数据连接
dbConnect.dbConnect();
//启用bodyParser
app.use(bodyParser.urlencoded({extend:true}));
//启用cookie
var keys = [];
for(var i=0;i<100;i++){
    var str = 'abcd133%%^&'+Math.random(1000000);
    keys.push(str);
}
app.use(cookiesParser());
app.use(cookieSession({
    name:'storeOnline',
    keys:keys,
    maxAge:20*60*1000
}))


//启用路由
app.use('/slide',slideRouter.router);
app.use('/user',userRouter.router);
app.use('/views',viewRouter.router);
app.use('/child',childRouter.router);
app.use('/men',menRouter.router);
app.use('/women',womenRouter.router);
app.use('/mob',mobileService.router);
//起始页
//路由链
app.use('/',function (req,res,next) {
    var options = {
        root:__dirname + '/www/',
        dotfiles:'deny',
        headers:{
            'x-timestamp':Date.now(),
            'x-sent':true
        }
    };
    var url = req.url;
    //根文件夹与非跟文件夹的操作
    if(url.indexOf('/')==0 && url.length>1){
        next();
    }else{
        res.sendFile('index.html',options,function (err) {
            if(err){
                console.log('index.html is error!');
            }
        })
    }

});

//静态页面的访问
app.use(expressStatic('./www'));